<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:rich="http://richfaces.org/rich"
	xmlns:a4j="http://richfaces.org/a4j"
	xmlns:s="http://jboss.com/products/seam/taglib"
	xmlns:o="http://openfaces.org/"
	template="/WEB-INF/esquema/permitTemplate.xhtml">

	<ui:define name="activityCustomContent">
		<h3>#{messages['options.workflow']}</h3>
		<br/>
		<a4j:commandLink value="#{messages['options.view_request']}"
			oncomplete="#{rich:component('viewPanel')}.show()">
			<f:setPropertyActionListener value="#{true}" target="#{viewPermitHelper.renderData}" />
		</a4j:commandLink>

		<br/>
		<br/>
		<br/>
		<rich:separator height="3px"/>
		<br/>
		
		<ui:include src="/permits/includes/viewPermitPanel.xhtml">
			<ui:param name="permitType" value="OPERATION_PERMIT" />
		</ui:include>
	</ui:define>

	<ui:define name="mainControls">
		<o:commandButton value="#{messages['button.ok']}" onclick="finish();" />
		<o:commandButton value="#{messages['button.l8r']}" onclick="pause();" />
	</ui:define>

	<ui:define name="extraControls">
		<o:commandButton value="#{messages['permit.spatial_analysis']}" onclick="#{rich:component('spatialAnalysisPanel')}.show();" />
	</ui:define>
	
	<ui:define name="body">
		<div align="center">
			<br/>
			<strong>#{messages['operation_permit.analysis_header']}</strong>
			<rich:separator lineType="solid"/>
			<br/>

			<ui:include src="/WEB-INF/includes/showHelpIncl.xhtml">
				<ui:param name="width" value="500" />
				<ui:param name="mainMessage" value="#{messages['permit.analysis_message']}" />
			</ui:include>

			<br/>
		</div>
		<h:form>
			<h:panelGroup>
				<div align="center">
					<br/>
					<strong>#{messages['operation_permit.analysis_permit_type']}</strong>
					<rich:separator lineType="solid"/>
					<br />
					<h:panelGrid columns="2">
						<h:outputText value="#{messages['permit.type']}" />
						<h:selectOneMenu value="#{operationPermitHelper.permit.type}">
							<f:selectItems value="#{operationPermitHelper.permitTypeValueList}"/>
							<a4j:support event="onkeyup" reRender="subType"/>
							<a4j:support event="onchange" reRender="subType"/>
						</h:selectOneMenu>
					</h:panelGrid>
				</div>
				<br/>
				<br/>
				<div align="center">
					<br/>
					<strong>#{messages['permit.target_zone']}</strong>
					<rich:separator lineType="solid"/>
					<br/>
					<a4j:commandLink ajaxSingle="true" oncomplete="#{rich:component('selectZonePanel')}.show()">
						<h:outputText value="#{messages['permit.select_zone']}"/>
					</a4j:commandLink>
					<br />
					<br />
					<br />
					<h:panelGroup id="grpZone">
						<ui:include src="/WEB-INF/includes/zoneInfoIncl.xhtml">
							<ui:param name="zone" value="#{operationPermitHelper.permit.spatialZone}" />
							<ui:param name="noZoneMsg" value="#{messages['permit.no_zone_selected']}" />
							<ui:param name="width" value="50%" />
						</ui:include>
					</h:panelGroup>
				</div>
				<br />
				<br />
			</h:panelGroup>
			
			<br />
			<br />
			
			<h:panelGroup id="grpFilledObservations">
				<h:panelGroup rendered="#{empty spatialAnalysisHelper.spatialRuleToAnalysisMap and not empty operationPermitHelper.permitObservations}">
					<div align="center">
						<br />
						<ui:include src="/permits/includes/analystObservationsIncl.xhtml">
							<ui:param name="title" value="#{messages['permit.analysis_observations_by_spatial_rule']}" />
							<ui:param name="permitObservations" value="#{operationPermitHelper.permitObservations}" />
							<ui:param name="readonly" value="false" />
						</ui:include>
					</div>
					<br />
					<br />
				</h:panelGroup>
			</h:panelGroup>
			
			<div align="center">
				<br/>
				<strong>#{messages['permit.spatial_analysis']}</strong>
				<rich:separator lineType="solid"/>
				<br />
				<a4j:commandLink value="#{messages['permit.do_spatial_analysis']}" onclick="#{rich:component('spatialAnalysisPanel')}.show();" />
				<br />
				<br />
				<br />
				<h:panelGroup id="grpSpatialAnalysis">
					<h:panelGroup rendered="#{spatialAnalysisHelper.ruleGroup == null}">
						<h:outputText value="#{messages['permit.no_spatial_rule_selected']}" style="color:Red;" />
					</h:panelGroup>
					<h:panelGroup rendered="#{spatialAnalysisHelper.ruleGroup != null}">
						<h:panelGroup rendered="#{operationPermitHelper.permit.spatialZone == null}">
							<h:outputText value="#{messages['permit.no_zone_selected']}" style="color:Red;" />
						</h:panelGroup>
						<h:panelGroup rendered="#{operationPermitHelper.permit.spatialZone != null}">
							<ui:repeat var="rule" value="#{spatialAnalysisHelper.ruleGroupRules}">
								<rich:panel>
									<table width="50%" style="text-align: left;">
										<tr>
											<th width="30%">#{messages['spatial_rules.code']}</th>
											<td width="70%">#{rule.code}</td>
										</tr>
										<tr>
											<th>#{messages['spatial_rules.description']}</th>
											<td>#{rule.description}</td>
										</tr>
									</table>
									<br />
									
									<rich:dataTable var="sar" 
										value="#{spatialAnalysisHelper.sarListFromRule(rule)}"
										width="95%">
										<rich:column>
											<f:facet name="header">#{messages['spatial_analysis.zone_name']}</f:facet>
											<h:outputText value="#{sar.zoneName}" />
										</rich:column>
										<rich:column>
											<f:facet name="header">#{messages['spatial_analysis.location']}</f:facet>
											<h:outputText value="#{sar.locationInCountry}" />
										</rich:column>
										<rich:column>
											<f:facet name="header">#{messages['spatial_analysis.current_land_use']}</f:facet>
											<h:outputText value="#{sar.primaryLandUse}" />
										</rich:column>
										<rich:column>
											<f:facet name="header">#{messages['spatial_analysis.proposed_land_use']}</f:facet>
											<h:outputText value="#{sar.secondaryLandUse}" />
										</rich:column>
										<rich:column>
											<f:facet name="header">#{messages['spatial_analysis.level']}</f:facet>
											<h:outputText value="#{sar.levelName}" />
										</rich:column>
										<rich:column rendered="#{rule.distanceRule}">
											<f:facet name="header">#{messages['DISTANCE']}</f:facet>
											<h:outputText value="#{sar.distance}" />
										</rich:column>
										<rich:column breakBefore="true" style="text-align: center;" colspan="#{rule.distanceRule ? 6 : 5}">
											<h:outputText value="#{messages['permit.analyst_opinion']}" style="color:Red;font-weight:bold;" />
											<h:inputTextarea value="#{sar.analystObservation}" style="width:95%"/>
										</rich:column>
									</rich:dataTable>
								</rich:panel>
								<br />
								<br />
							</ui:repeat>
						</h:panelGroup>
					</h:panelGroup>
				</h:panelGroup>
			</div>
			
			<br />
			<br />
			
			<div align="center">
				<br/>
				<strong>#{messages['permit.analyst_opinion']}</strong>
				<rich:separator lineType="solid"/>
				<br/>
				<h:inputTextarea
					value="#{operationPermitHelper.permit.analystOpinion}"
					rows="5" style="width:75%">
				</h:inputTextarea>
			</div>
			
			<br />
			<br />
			
			<a4j:jsFunction name="finish" action="#{operationPermitHelper.endAnalysisTask}" />
		</h:form>
		
		<h:form>
			<a4j:jsFunction name="pause" action="home" />
		</h:form>
		
		
		<ui:include src="/zoneselector/selectZonePanel.xhtml">
			<ui:param name="width" value="800" />
			<ui:param name="height" value="610" />
			<ui:param name="selectAction" value="operationPermitHelper.selectZoneFromMap" />
			<ui:param name="render" value="grpZone" />
		</ui:include>
		
		
		<ui:include src="/permits/includes/spatialAnalysisPanel.xhtml">
			<ui:param name="render" value="grpSpatialAnalysis,grpFilledObservations" />
		</ui:include>
	</ui:define>
	
</ui:composition>
